#pragma once
#include "CharClass.h"

class TransitionTable
{
public:
	TransitionTable(int R,int C) : RowNumber(R),ColNumber(C)
	{
		Matrix = (int**)new int*[RowNumber];
		for(int i=0;i<RowNumber;i++)
			Matrix[i] = new int[ColNumber];
		for(int i=0;i<RowNumber;i++)
			for(int j=0;j<ColNumber;j++)
				Matrix[i][j] = 0;
	}

	~TransitionTable()
	{
		Clear();
	}

	void SetValue(int i,int j,int Value)
	{
		Matrix[i][j] = Value;
	}

	int GetValue(int i,int j)
	{
		return Matrix[i][j];
	}

	int Transit(int current,WCHAR input,const List<NAutoPtr<CharClass>>& CharList)
	{
		List<int> L;
		for(int i=0,n=CharList.Size();i<n;i++)
		{
			if(CharList[i]->IsClass(input))
				L.Add(Matrix[current][i]);
		}
		int Size = L.Size();
		if(Size)
			return L[Size - 1];
		return -1;
	}

	void TransitionTable::Clear(void)
	{
		for(int i=0;i<RowNumber;i++)
		{
			delete[] Matrix[i];
		}
		delete Matrix;
	}
protected:
	int** Matrix;
	int RowNumber;
	int ColNumber;
};